import { Component, ViewChild } from '@angular/core';
import { Platform, Nav, ToastController } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

import { MainTab } from '../pages/main-tab/main-tab';

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage: any = MainTab;
  @ViewChild(Nav) nav: Nav;
  toast: any = ToastController;
  backButtonPressed: boolean = false;  //用于判断返回键是否触发

  initializeApp() {
    this.platform.ready().then(() => {
      this.statusBar.styleDefault();
      this.splashScreen.hide();
      // 返回按键事件
      this.platform.registerBackButtonAction((): any => {
        let activeVC = this.nav.getActive();
        let page = activeVC.instance;
        if (!(page instanceof MainTab)) {
          if (!this.nav.canGoBack()) {
            // 当前页面为tabs，退出APP
            return this.showExit();
          }
          // 当前页面为tabs的子页面，正常返回
          return this.nav.pop();
        }
        let tabs = page.tabs;
        let activeNav = tabs.getSelected();
        if (!activeNav.canGoBack()) {
          // 当前页面为tab栏，退出APP
          return this.showExit();
        }
        // 当前页面为tab栏的子页面，正常返回
        return activeNav.pop();
      }, 101);
    });
  }

  showExit() {
    if (this.backButtonPressed) this.platform.exitApp();  // 当触发标志为true时，即2秒内双击返回按键则退出APP
    else {
      let toast = this.toast.create({
        message: '再按一次退出应用',
        duration: 2000,
        position: 'bottom'
      });
      toast.present();
      this.backButtonPressed = true;
      // 2秒内没有再次点击返回则将触发标志标记为false
      setTimeout(() => {
        this.backButtonPressed = false;
      }, 2000);
    }
  }

  constructor(
    public platform: Platform,
    public statusBar: StatusBar,
    public splashScreen: SplashScreen
  ) {
    this.initializeApp();
  }

}
